package com.wwwarehouse.contract.order_information;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.AdapterView;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.ConvertUtils;
import com.wwwarehouse.contract.adapter.BuyAdapter;
import com.wwwarehouse.contract.bean.BuyBean;
import com.wwwarehouse.contract.core.ContractConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;

/**
 * Creator by zhenwang.xiang on 2017/11/2 16:01
 * Description: 我买到的Fragment
 */


public class BuyFragment extends BaseFragment {
    private ListView mListView;
    private int mPage;
    private String mSort;
    private String mBusinessName;
    private int mOrderType;
    private ArrayList<String> mBusinessIds;
    private ArrayList<String> mOrderStateList;
    private StateLayout mStateLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_buy, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mListView = $(R.id.list_view);
        mStateLayout = $(R.id.state_layout);
        mStateLayout.setAllContentViewMargin(ConvertUtils.dip2px(getActivity(), 11), ConvertUtils.dip2px(getActivity(), 5), ConvertUtils.dip2px(getActivity(), 11), 0);
        mStateLayout.showProgressView(true);
        if (getArguments() != null) {
            mPage = getArguments().getInt("page");
            mBusinessIds = getArguments().getStringArrayList("businessIds");
            mBusinessName = getArguments().getString("businessName");
            mOrderStateList = getArguments().getStringArrayList("orderStatus");
            mOrderType = getArguments().getInt("orderType");
            mSort = getArguments().getString("sort");
        }
        httpRequest();
    }


    private NoHttpUtils.OnResponseListener mOnResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            switch (what) {
                case 0:
                    if ("0".equals(commonClass.getCode())) {
                        final BuyBean bean = JSON.parseObject(commonClass.getData().toString(), BuyBean.class);
                        if (null != bean) {
                            mListView.setAdapter(new BuyAdapter(bean.getList(), getActivity(), mListView));
                            mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                    BuyOrderDetailsFragment buyOrderDetailsFragment = new BuyOrderDetailsFragment();
                                    Bundle bundle = new Bundle();
                                    bundle.putInt("orderType",mOrderType);
                                    bundle.putString("contractUkid", bean.getList().get(position).getContractUkid());
                                    buyOrderDetailsFragment.setArguments(bundle);
                                    pushFragment(buyOrderDetailsFragment);
                                }
                            });
                        }
                    } else {
                        mStateLayout.showSystemView(commonClass.getMsg(), true);
                        mStateLayout.setSystemListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                httpRequest();
                            }
                        });
                    }
                    break;
                default:
                    break;
            }
        }

        @Override
        public void onFailed(String result, int what) {
            mStateLayout.showSystemView(result, true);
            mStateLayout.setSystemListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    httpRequest();
                }
            });
        }

        @Override
        public void onFinish(int what) {
            mStateLayout.showContentView();
        }
    };


    @Override
    public void setTitle() {
        if (peekFragment() instanceof BuyFragment) {
            if (mOrderType == 1) {
                mActivity.setTitle("我买到的");
            } else if (mOrderType == 2) {
                mActivity.setTitle("我卖出的");
            }
        }
    }

    @Override
    public void requestDatas() {

    }

    public void httpRequest() {
        Map<String, Object> map = new HashMap<>();
        map.put("businessIds", mBusinessIds);
        map.put("orderType", mOrderType);
        map.put("page", mPage);
        map.put("size", "5");
        map.put("sort", mSort);

        if (!TextUtils.isEmpty(mBusinessName)) {
            map.put("businessName", mBusinessName);
        }
        if (mOrderStateList.size() > 0) {
            map.put("orderStatus", mOrderStateList);
        }
        NoHttpUtils.httpPost(ContractConstant.GET_MYORDER, map, mOnResponseListener, 0);
    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }
}
